package cn.sad.bjsx.yjfk.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.sad.zeroboot.core.contants.CoreConstants;
import cn.sad.zeroboot.core.controller.BaseController;
import cn.sad.zeroboot.core.po.AjaxRes;
import cn.sad.zeroboot.core.po.DbSaveParam;
import cn.sad.zeroboot.core.po.auth.LoginUser;
import cn.sad.zeroboot.core.po.fj.FjSaveParam;
import cn.sad.zeroboot.core.po.query.QueryParam;
import cn.sad.zeroboot.core.po.query.QueryRes;
import cn.sad.zeroboot.core.util.ConvertUtil;
import cn.sad.zeroboot.core.util.DateUtil;
import cn.sad.zeroboot.core.util.HttpUtil;
import cn.sad.zeroboot.core.util.LoginUserUtil;
import cn.sad.zeroboot.core.util.query.QuerySettingUtil;
import cn.sad.zeroboot.core.util.query.SqlConditionBuilderUtil;

@RestController
@RequestMapping(YjfkYddController.MAPPINGNAME)
public class YjfkYddController extends BaseController {

	public static final String MAPPINGNAME = "yjfk/yjfkYdd";

	/**
	 * 首页
	 */
	@RequestMapping("index")
	public ModelAndView index(HttpServletRequest request) {
		return forward(MAPPINGNAME, "index");
	}

	/**
	 * 查询
	 */
	@RequestMapping("query")
	public QueryRes query(HttpServletRequest request, HttpServletResponse response) {
		QueryParam param = new QueryParam(this.buildSql(request));

		Map<String, String> dicMap = new HashMap<>();
		dicMap.put("FKFLGID", "bjsx.yjfkfl");
		param.setDicMap(dicMap);

		Map<String, String> fmtMap = new HashMap<>();
		fmtMap.put("CJSJ", DateUtil.YYYY_MM_DD_HH_MM);
		param.setFmtMap(fmtMap);

		return baseService.queryOrExport(param);
	}

	/**
	 * 保存意见反馈
	 */
	@RequestMapping("save")
	@Transactional(rollbackFor = Exception.class)
	public AjaxRes save(HttpServletRequest request) {
		LoginUser loginUser = LoginUserUtil.getLoginUser();

		Map<String, Object> reqDatas = HttpUtil.getReqDatas(request, "yjfl");

		// 保存附件
		String picJson = request.getParameter("picJson");
		AjaxRes fjRes = fjService.savePicFile(picJson, new FjSaveParam(ConvertUtil.strOf(reqDatas.get("FJBH")), null, "yjfk"));
		if (fjRes.getCode() != CoreConstants.SUCCESS) {
			return fjRes;
		}
		String fjbh = (String) fjRes.getData();
		reqDatas.put("FJBH", fjbh);

		Map<String, Object> addAppendData = new HashMap<>();
		addAppendData.put("CJR", loginUser.getUserId());
		addAppendData.put("CJRXM", loginUser.getUserName());
		addAppendData.put("CJSJ", new Date());
		addAppendData.put("SFYHF", 0); // 是否已回复

		DbSaveParam saveParam = new DbSaveParam(null, "d_bjsx_yjfk_nr");
		saveParam.setAddAppendData(addAppendData);
		jdbcService.save(saveParam, reqDatas);

		return AjaxRes.success();
	}

	private String buildSql(HttpServletRequest request) {
		LoginUser loginUser = LoginUserUtil.getLoginUser();

		String sql = "select gid, fkflgid, sj, yx, fjbh, bt, nr, cjsj, sfyhf from d_bjsx_yjfk_nr t where cjr='" + loginUser.getUserId() + "'";
		sql += QuerySettingUtil.buildSqlCondition();
		sql += SqlConditionBuilderUtil.autoBuildSqlCondition(request, "t");
		sql += " order by cjsj desc";
		return sql;
	}
}
